//
//  AppDelegate.m
//  TripTogether
//
//  Created by dllo on 15/12/21.
//  Copyright © 2015年 王军岐. All rights reserved.
//

#import "AppDelegate.h"
#import "WJQTabBarController.h"

#import "YHYRecommendViewController.h"
#import "YHYHunterCityViewController.h"
#import "WJQViewController3.h"
#import "ZLViewController.h"
#import "WJQUserViewController.h"

#import <BmobSDK/Bmob.h>
#import <SMS_SDK/SMSSDK.h>

#import "FirstLaunchViewController.h"


@interface AppDelegate ()<UIScrollViewDelegate>

@property (nonatomic, retain) UINavigationController *RecommendNC;
@property (nonatomic, strong) WJQTabBarController *tabbar;
@property (nonatomic, assign) BOOL isOut;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    sleep(1);
    self.isOut = NO;
    
    [Bmob registerWithAppKey:@"8e8f34b865ebe2f7e5afd09256ded362"];
    //share短信验证 初始化应用，appKey和appSecret从后台申请得
    [SMSSDK registerApp:@"e25e4e8aad68"
             withSecret:@"ab3441584e96962c92c9ef52059fbb29"];
    
    // 是否第一次启动
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"isFirst"]) {
        
        [self makeView];
        
    } else {
        [self goMain];
    }
    
 
    //状态栏字体颜色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    return YES;
    
}
- (void)makeView{
    
   
    FirstLaunchViewController *firsVC = [[FirstLaunchViewController alloc] init];
    self.window.rootViewController = firsVC;
    
    NSArray *arr = [NSArray arrayWithObjects:@"wode_morenbg@2x.jpg", @"wode_morenbg@2x.jpg",@"wode_morenbg@2x.jpg",nil];
    
    UIScrollView *scr = [[UIScrollView alloc]initWithFrame:self.window.bounds];
    scr.backgroundColor = [UIColor clearColor];
    scr.contentSize = CGSizeMake(SCREEN_WIDTH * arr.count, self.window.bounds.size.height);
    scr.pagingEnabled = YES;
    scr.tag = 7000;
    scr.delegate = self;
    [self.window addSubview:scr];
    
    for (int i = 0; i < arr.count; i++) {
        
        UIImageView *imag = [[UIImageView alloc]initWithFrame:CGRectMake(i * SCREEN_WIDTH, 0, SCREEN_WIDTH, self.window.frame.size.height)];
        imag.image = [UIImage imageNamed:arr[i]];
        [scr addSubview:imag];
        
    }
    
    
}
#pragma mark scrollView的代理
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //当图片全部滑完后 又像后多滑了10 的时候就做下一个动作
    if (scrollView.contentOffset.x > 2 * SCREEN_WIDTH + 10) {
        
        self.isOut=YES;//这是我声明的一个全局变量Bool 类型的，初始值为no，当达到我需求的条件时将值改为yes
    }
}
//停止滑动
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //判断isout为真 就要进入主界面了
    if (self.isOut) {
        //添加一个动画
        [UIView animateWithDuration:1.5 animations:^{
            scrollView.alpha=0;//让scrollview 渐变消失
            
        }completion:^(BOOL finished) {
            //            [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"isFirst"];
            
            [scrollView  removeFromSuperview];//将scrollView移除
            [self goMain];
        } ];
    }
    
}

- (void)goMain{
    
    FirstLaunchViewController *firstVC = [[FirstLaunchViewController alloc] init];
    firstVC.tabBarController.hidesBottomBarWhenPushed = YES;


    YHYRecommendViewController *vc1 = [[YHYRecommendViewController alloc] init];
    _RecommendNC = [[UINavigationController alloc] initWithRootViewController:vc1];
    _RecommendNC.navigationBar.translucent = NO;
    
    
    YHYHunterCityViewController *hunterCityVC = [[YHYHunterCityViewController alloc] init];
    UINavigationController *hunterCityNC = [[UINavigationController alloc] initWithRootViewController:hunterCityVC];
    hunterCityNC.navigationBar.translucent = NO;
    
    ZLViewController *zlVC = [[ZLViewController alloc] init];
    UINavigationController *zlNavC = [[UINavigationController alloc] initWithRootViewController:zlVC];
    zlNavC.navigationBar.translucent = NO;
    
    WJQUserViewController *userVC = [[WJQUserViewController alloc] init];
    UINavigationController *userNC = [[UINavigationController alloc] initWithRootViewController:userVC];
    userNC.navigationBar.translucent = NO;
    
    self.tabbar = [[WJQTabBarController alloc] init];
    self.tabbar.tabBar.tintColor = [UIColor yellowColor];
    self.tabbar.viewControllers = [NSArray arrayWithObjects:_RecommendNC, hunterCityNC, zlNavC, userNC, nil];

    self.window.rootViewController = self.tabbar;
    
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
